import React from "react"
import './Consult.css'
import { Button , Table} from 'antd';
import { withRouter, Link} from "react-router-dom";
import Recruitment from '../Recruitment/Recruitment'
import Register from '../Register/Register'

class Consult extends React.Component {
  rua(){
    console.log(1111)
    console.log(this.props)
  }
  state = {
    selectedRowKeys: [], // Check here to configure the default column
    loading: false,
    tableData: [],
    //已选择的个数
    allNum:0
  };
  componentWillMount() {
    window._myFlag=true
    console.log(window._myFlag)
  }
  start = () => {
    this.setState({ loading: true });
  // ajax request after empty completing
    setTimeout(() => {
        this.setState({
        selectedRowKeys: [],
        loading: false,
        //总数
        allNumber:0,
        //传递过去的Id
        transmitId:""
        });
    }, 1000);
  }
  onSelectChange = (selectedRowKeys) => {
    console.log('selectedRowKeys changed: ', selectedRowKeys);
    this.setState({ 
      selectedRowKeys: selectedRowKeys,
      allNumber:selectedRowKeys.length
    });
  }
  test(key) {
    console.log(key)
    this.setState({
      transmitId:key
    })
  }
  //多选清空&&单清空
  clear(arr){
    let arrSplice = this.state.tableData
    if(typeof arr == "number"){
      console.log("number")
      fetch('/zhongcai/information/'+arr,{
        method:"DELETE"
      }).then((res)=>{
        return res.json();
      }).then(res => {
        console.log(res,"成功")
      }).catch((res)=>{
          console.log(res);
      });
    }
    else {
      for(var i=0;i<arrSplice.length;i++){
        for(var j=0;j<arr.length;j++){
          if(arrSplice[i].key === arr[j] ){
            arrSplice.splice(i,1)
            fetch('/zhongcai/information/'+arr[j],{
              method:"DELETE"
            }).then((res)=>{
              return res.json();
            }).then(res => {
              console.log(res,"成功")
            }).catch((res)=>{
                console.log(res);
            });
          }
        }
      }
    }
    
   
    this.setState({
      tableData:arrSplice
    })
  }
  componentDidMount(){
     //所有列表
    //http://120.26.61.246:9002/zhongcai/information/?pageSize=10
    fetch('/zhongcai/information/?pageSize=10').then((res)=>{
      return res.json();
    }).then(res => {
      let data = res.data.pageInfo.list
      let tableData = this.state.tableData
      
      data.forEach((item, index) => {
        let obj = {
          key: item.id,
          name: item.content,
          age: item.title,
          address: item.createat
        }
        tableData[index] = obj
      });
      this.setState({
        tableData
      })
    }).catch((res)=>{
        console.log(res);
    });
  }
  render() {

    const columns = [{
      title: '分类',
      dataIndex: 'name',
    }, {
      title: '标题',
      dataIndex: 'age',
    }, {
      title: '时间',
      dataIndex: 'address',
    },{
      title: '操作',
      dataIndex: 'key', key: 'x', render: (data, record) => 
        <div>
          <Link to={
              {
                pathname:"/ConsultEdit", 
                state: {data:`${record.key}`}  
              }
          }
          ><Button icon="form" style={{marginRight:"12px",outline: "none",border: "none",boxShadow: "none"}} onClick={this.test.bind(this,record.key)}>修改</Button></Link>
          <Button icon="delete" style={{marginRight:"12px",outline: "none",border: "none",boxShadow: "none"}} onClick={this.clear.bind(this,record.key)}>删除</Button>
          <Button icon="double-right" style={{marginRight:"12px",outline: "none",border: "none",boxShadow: "none"}}>查看</Button>
        </div>
    }
  ];
   

    const { selectedRowKeys } = this.state;
    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange,
    };
    return (
      <div>
          <Button type="primary" icon="plus" onClick={this.rua.bind(this)}>新建</Button>
          <div className="info">
            <Button icon="exclamation-circle" style={{border:"none",outline:"none",boxShadow:"none",color:"#2a8ff6",backgroundColor:"#e5f7ff"}}></Button>
            <span>已选择<span className="addNumber">{this.state.allNumber || 0}</span>项</span>
            <span className="clearAll" onClick={this.clear.bind(this,this.state.selectedRowKeys)}>清空</span>
          </div>
          <div>
          <div style={{ marginBottom: 16 }}>
            <span style={{ marginLeft: 8 }}>
            </span>
            </div>
            <Table rowSelection={rowSelection} columns={columns} dataSource={this.state.tableData} />
          </div>
      </div>
    );
  }
}



function Home() {
  return (
    <div>
      {/* <ConsultEdit></ConsultEdit> */}
      {/* 开始进去的页面 */}
      <Consult></Consult>
    </div>
  );
}
function About() {
  return (
    <div>
      <Recruitment></Recruitment>
    </div>
  );
}
function Topics() {
  return (
    <div>
      <Register></Register>
    </div>
  );
}

export default withRouter(Consult);

